﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Base;

namespace SalesSys.DAL.Models
{
    /// <summary>
    /// 供货商类型
    /// </summary>
    public enum ProviderType
    {
        /// <summary>
        /// 生产商
        /// </summary>
        Producer = 0,
        /// <summary>
        /// 仓库销售
        /// </summary>
        Depot = 1,
        /// <summary>
        /// 贸易商
        /// </summary>
        Merchant=2
    }

    /// <summary>
    /// 供货商
    /// </summary>
    public class Provider : DbEntityBase<Guid>
    {
        /// <summary>
        /// 供应商类型
        /// </summary>
        public ProviderType ProviderType { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public virtual District District { get; set; }

        /// <summary>
        /// 先款优惠
        /// </summary>
        public decimal FullPaymentDiscount { get; set; }

        /// <summary>
        /// 非标优惠数
        /// </summary>
        public decimal NonstandardDiscount { get; set; }

        /// <summary>
        /// 采购非标优惠金额,用于统一分析进销成本
        /// </summary>
        public decimal PurchasingNonstandard { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 非标优惠特例，供应商id=优惠；供应商id=优惠；
        /// </summary>
        public string NonstandardDiscountCase { get; set; }

        public decimal? GetNonstandardDiscountCase(Guid supplierId)
        {
            var dic = SimpleJson.Deserialize<Dictionary<Guid, decimal>>(NonstandardDiscountCase);
            if (dic.ContainsKey(supplierId))
            {
                return dic[supplierId];
            }
            return null;
        }

        public IDictionary<Guid, decimal> GetNonstandardDiscountDic()
        {
            if (string.IsNullOrWhiteSpace(NonstandardDiscountCase))
            {
                return new Dictionary<Guid, decimal>();
            }
            return SimpleJson.Deserialize<Dictionary<Guid, decimal>>(NonstandardDiscountCase);
        }

        public string SetNonstandardDiscountCase(IDictionary<Guid, decimal> dic)
        {
            if (dic == null)
            {
                NonstandardDiscountCase = null;
            }
            else
            {
                NonstandardDiscountCase = SimpleJson.Serializer(dic);
            }

            return NonstandardDiscountCase;
        }


        /// <summary>
        /// 仓库商品
        /// </summary>
        public virtual ICollection<DepotCommodity> DepotCommoditys { get; set; }
       /// <summary>
       /// 仓库流水
       /// </summary>
        public virtual ICollection<DepotRecord> DepotRecords { get; set; }

        /// <summary>
        /// 钢厂
        /// </summary>
        public virtual Supplier Supplier { get; set; }
        /// <summary>
        /// 区域经理
        /// </summary>
        public virtual Employee Manager { get; set; }
        /// <summary>
        /// 运营人员
        /// </summary>
        public virtual Employee OperationsMan { get; set; }

        /// <summary>
        /// 是否非标
        /// </summary>
        public bool IsNonStandard { get; set; }
        /// <summary>
        /// 驻厂人员列表
        /// </summary>
        public virtual ICollection<Employee> FieldMans { get; set; }

        /// <summary>
        /// 出库单流水
        /// </summary>
        public virtual ICollection<DeliveryList> DeliveryLists { get; set; }

        /// <summary>
        /// 入库单流水
        /// </summary>
        public virtual ICollection<WarehouseEntry> WarehouseEntrys { get; set; }
        /// <summary>
        /// 主要驻厂人员
        /// </summary>
        public virtual Employee MainFieldMan { get; set; }
        public class Mapper : PurMapper<Provider, Guid>
        {
            public Mapper()
            {

            }
        }
    }
    /// <summary>
    /// 仓储流水
    /// </summary>
    public class DepotRecord : DbEntityBase<Guid>
    {
        public class Mapper : PurMapper<DepotRecord, Guid>
        {
            public Mapper()
            {

            }
        }
        public enum RecordType
        {
            /// <summary>
            /// 出库
            /// </summary>
            Send = 0,
            /// <summary>
            /// 入库
            /// </summary>
            Into=1,
            /// <summary>
            /// 冻结
            /// </summary>
            Lock=2,
            /// <summary>
            /// 解冻
            /// </summary>
            Unlock=3
        }
        public DepotRecord() {
            CreateTime = DateTime.Now;
        }
        /// <summary>
        /// 记录类型
        /// </summary>
        public RecordType Type { get; set; }
        #region 入库
        /// <summary>
        /// 入库，已发出重量
        /// </summary>
        public decimal SendedWeight { get; set; }
        ///// <summary>
        ///// 入库，已发数量
        ///// </summary>
        //public decimal SendedCount { get; set; }
        /// <summary>
        /// 单价,每吨
        /// </summary>
        public decimal Price { get; set; }
        /// <summary>
        /// 单重
        /// </summary>
        public decimal Weight { get; set; }
      
        #endregion
        /// <summary>
        /// 出库管理，出库单商品id,
        /// </summary>
        public Guid? ToId { get; set; }
        /// <summary>
        /// Id,入库或出库单中，对应商品
        /// </summary>
        public Guid FromId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 供应商
        /// </summary>
        public virtual Provider Provider { get; set; }

        /// <summary>
        /// 供应商编号
        /// </summary>
        public Guid ProviderId { get; set; }

        /// <summary>
        /// 商品
        /// </summary>
        public virtual Commodity Commodity { get; set; }

        /// <summary>
        /// 商品编号
        /// </summary>
        public Guid CommodityId { get; set; }
       
        /// <summary>
        /// 商品数量,只用于盘库，手动填入，入库为正，出库为负
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 总重，入库为正，出库为负
        /// </summary>
        public decimal TotalWeight { get; set; }
        /// <summary>
        /// 入库前总重
        /// </summary>
        public decimal BeforeTotalWeight { get; set; }
        /// <summary>
        /// 入库前总库存数量
        /// </summary>
        public int BeforeTotalCount { get; set; }
        /// <summary>
        /// 入库后总重
        /// </summary>
        public decimal AfterTotalWeight { get; set; }
        /// <summary>
        /// 入库后总数量
        /// </summary>
        public int AfterTotalCount { get; set; }

    }
}
